package br.com.ss.seguranca.controller.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import br.com.ss.seguranca.model.entity.Sistema;
import br.com.ss.seguranca.model.entity.User;

@Component("sistemaService")
public class SistemaServiceImp {

	@PersistenceContext
	private EntityManager entityManager;

	public List<Sistema> findAll() {
		try {
			return entityManager.createNamedQuery("Sistema.findAllSistemas",
					Sistema.class).getResultList();

		} catch (NoResultException e) {
			throw new UsernameNotFoundException("Usuario nao encontrado");
		}
	}

	public List<Sistema> findSistemaUsuario( User user ) {
		try {
			return entityManager
					.createNamedQuery("Sistema.findSistemaByUser",
							Sistema.class).setParameter("user", user)
					.getResultList();

		} catch (NoResultException e) {
			throw new UsernameNotFoundException("Usuario nao encontrado");
		}
	}

}
